<?php
$page_name=basename($_SERVER['SCRIPT_FILENAME']);
$IsPreserved	= 'Y';
$IsProcess		= $_REQUEST['IsProcess'];
include("general_include.php");
include "checklogin.php";
//if($_SESSION['user_type']=='2')
 // header("location:student_game.php");
if($IsProcess <> 'Y')
{
	include "top.php";
	//include "left.php";
}	

$action	= $_REQUEST['action'];

if($action=="del")
{	
	if(!empty($delete_id))
	{
	
	  $DelSql = "DELETE FROM ".TABLEPREFIX."_user_game where user_game_id='$delete_id'";
	  $UserManagerObjAjax->Execute($DelSql);
	  
    }	  	 
}  

function findvalue($table,$fld_name,$fld_value,$find_value)
{
     if($fld_value=='')
			return false;
			
		$SelectTypeSql = "SELECT ".$find_value." FROM ".$table." WHERE $fld_name=".$fld_value;
		$SelectType = mysql_fetch_array(mysql_query($SelectTypeSql));
		return $SelectType[0];
}	


$CoachgameSql = "SELECT * FROM ".TABLEPREFIX."_user_game where user_id='".$_SESSION['user_id']."' and user_type_id='1' ORDER BY date_added DESC";
$CoachgameArr = $UserManagerObjAjax->GetRecords("All",$CoachgameSql);
$Numcoachgame=count($CoachgameArr);

for($g=0;$g<$Numcoachgame;$g++)
{
	$rate_A = $CoachgameArr[$g]['rate_A'];
	$rate_B = $CoachgameArr[$g]['rate_B'];
	$rate_C = $CoachgameArr[$g]['rate_C'];
	
	if($rate_B == null || $rate_B == '' || $rate_B == 0)
		$CoachgameArr[$g]['rate_B'] = $rate_A;
		
	if($rate_C == null || $rate_C == '' || $rate_C == 0)
		$CoachgameArr[$g]['rate_C'] = $CoachgameArr[$g]['rate_B'];
	
    $gameSql = "SELECT * FROM ".TABLEPREFIX."_game WHERE game_id='".$CoachgameArr[$g]['game_id']."'";
	$GameArr = $UserManagerObjAjax->GetRecords("Row",$gameSql);
	$CoachgameArr[$g]['is_ladder'] = $GameArr['is_ladder'];
	$CoachgameArr[$g]['is_race'] = $GameArr['is_race'];
	$CoachgameArr[$g]['is_server'] = $GameArr['is_server'];
	$CoachgameArr[$g]['is_region'] = $GameArr['is_region'];
	$CoachgameArr[$g]['is_rating'] = $GameArr['is_rating'];
    
    $CoachgameArr[$g]['game']= findvalue(TABLEPREFIX."_game","game_id",$CoachgameArr[$g]['game_id'],"game_name");
	$CoachgameArr[$g]['ladder']= findvalue(TABLEPREFIX."_game_ladder","ladder_id",$CoachgameArr[$g]['ladder_id'],"ladder_name");
	
	$Coachrace = explode(",",$CoachgameArr[$g]['race_id']);
	$race ='';
	for($a=0;$a<count($Coachrace);$a++)
	{
	  if($Coachrace[$a]<>'')
	   $race .= findvalue(TABLEPREFIX."_game_race","race_id",$Coachrace[$a],"race_title").',';
	}
	$CoachgameArr[$g]['race'] = substr($race,0,-1);  
	
	$Coachserver = explode(",",$CoachgameArr[$g]['server_id']);
	$server ='';
	for($a=0;$a<count($Coachserver);$a++)
	{
	  if($Coachserver[$a]<>'')
	   $server .= findvalue(TABLEPREFIX."_game_server","server_id",$Coachserver[$a],"server_name").',';
	}
	$CoachgameArr[$g]['server'] = substr($server,0,-1);  
	
	$Coachregion = explode(",",$CoachgameArr[$g]['region_id']);
	$region ='';
	for($a=0;$a<count($Coachregion);$a++)
	{
	  if($Coachregion[$a]<>'')
	   $region .= findvalue(TABLEPREFIX."_game_region","region_id",$Coachregion[$a],"region_title").',';
	}
	$CoachgameArr[$g]['region'] = substr($region,0,-1);
	
	$Coachrating = explode(",",$CoachgameArr[$g]['rating_id']);
	$rating ='';
	for($a=0;$a<count($Coachrating);$a++)
	{
	  if($Coachrating[$a]<>'')
	   $rating .= findvalue(TABLEPREFIX."_game_rating","rating_id",$Coachrating[$a],"rating_title").',';
	}
	$CoachgameArr[$g]['rating'] = substr($rating,0,-1);
	
	$Coachrace[$g][$a] = substr($race,0,-1);  
	$CoachgameArr[$g]['experience']= stripslashes($CoachgameArr[$g]['experience']);

}
if($IsProcess == 'Y'){
	$Sqltype = "SELECT * FROM ".TABLEPREFIX."_user_type_user_relation WHERE user_id='".$_SESSION['user_id']."'";
	$typeArr = $UserManagerObjAjax->GetRecords("All",$Sqltype);
	$Numtype = count($typeArr);
	$is_coach = 0;
	$is_partner = 0;
	for($t=0;$t<$Numtype;$t++)
	{
	   if($typeArr[$t]['user_type_id']==1)
		  $is_coach = 1;
	   if($typeArr[$t]['user_type_id']==3)
		  $is_partner = 1;
	}
}	
$smarty->assign('is_coach',$is_coach);
$smarty->assign('is_partner',$is_partner);

$smarty->assign('Numcoachgame',$Numcoachgame);
$smarty->assign('CoachgameArr',$CoachgameArr);
$smarty->display('coach_game.tpl');
if($IsProcess <> 'Y')
{
	include "footer.php";
}	
?>